package com.puyuba.puyuba.view.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.puyuba.puyuba.R;
import com.pyb.java.cache.CacheUserInfo;
import com.pyb.java.cache.bean.UserInfoBean;
import com.pyb.java.commonutil.TabEntity;
import com.puyuba.puyuba.view.fragment.FindPrivateFragment;
import com.puyuba.puyuba.view.fragment.FindTransactionFragment;
import com.pyb.java.basemodule.BaseActivity;

import java.util.ArrayList;
/**
 * 我的用户页面
 */
public class MyUserActivity extends BaseActivity {
    
    private ImageView ivBack;
    private ImageView ivSearch;
    private EditText etSearch;
    private String[] mTitles = {"跟进用户","成交学员" ,"添加学员"};
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private CommonTabLayout tl2;
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
    private int positions;
    private FindPrivateFragment findPrivateFragment;
    private FindTransactionFragment findTransactionFragment;
    private UserInfoBean userInfoBean;

    @Override
    public int getLayoutId() {
        return R.layout.activity_myuser;
    }

    public static void startActivity(Context context,int id) {
        Intent intent = new Intent(context, MyUserActivity.class);
        intent.putExtra("id",id);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ivBack = (ImageView) findViewById(R.id.iv_back);
        tl2 = (CommonTabLayout) findViewById(R.id.tl_2);
        ivSearch = (ImageView) findViewById(R.id.iv_search);
        etSearch = (EditText) findViewById(R.id.et_search);

        initFragments();
        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i]));
        }
        tl2.setTabData(mTabEntities, this, R.id.fl_change, mFragments);
        tl2.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                if (position == 2){
                    AddStudentActivity.startActivity(MyUserActivity.this,1);
                }else {
                    positions = position;
                }
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
        positions = 0;
        tl2.setCurrentTab(positions);

        initListener();
        int id = getIntent().getIntExtra("id", 0);
        userInfoBean = CacheUserInfo.getInstance().getsUserInfo();

    }

    private void initListener() {
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        ivSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (positions == 0){
                    findPrivateFragment.initData(userInfoBean.getUser().getCrm_user_id(), etSearch.getText().toString().trim());
                }else {
                    findTransactionFragment.initData(userInfoBean.getUser().getCrm_user_id(), etSearch.getText().toString().trim());
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        tl2.setCurrentTab(0);
    }

    private void initFragments() {
        mFragments.clear();
        if (findPrivateFragment == null){
            findPrivateFragment = new FindPrivateFragment();
        }
        if (findTransactionFragment == null){
            findTransactionFragment = new FindTransactionFragment();
        }
        mFragments.add(findPrivateFragment);
        mFragments.add(findTransactionFragment);
    }



}
